<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/animate.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.min.css?v=4.1.0" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>用户管理</h5>
        </div>
        <div class="ibox-content">
            <div class="row row-lg">
                <div class="col-sm-12">
                    <div class="example-wrap">
                        <!-- start toolbar -->
                        <div class="col-sm-12">
                            <form class="form-horizontal" role="form" id="queryUserForm">
                                <div class="row">
                                 	<div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">用户名</label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control" placeholder="用户名" id="salesmanName" name="salesmanName">
                                            </div>
                                        </div>
                                    </div>
                                    <shiro:hasPermission name="company:getsysuser">
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">公司</label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control" placeholder="公司名" id="company" name="companyDto.comnumName">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-4 control-label">状态</label>
                                            <div class="col-sm-8">
                                                <select id="salesmanIsDelete" name="salesmanIsDelete" class="form-control">
                                                	<option value="">-----选择状态-----</option>
                                                	<option value="1">-----禁用-----</option>
                                                	<option value="0">-----启用-----</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    </shiro:hasPermission>
                                    <div class="col-sm-12" style="text-align:center;">
                                    	<button type="button" class="btn btn-primary" id="queryUserBtn">
                                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询
                                        </button>
                                        <button class="btn btn-info" id="addBut" type="button">新增用户</button>
                                        <button type="button" class="btn btn-info" id="" onclick="location.reload();">
                                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> 刷新
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <!-- end toolbar -->

                        <div class="example">
                            <table id="sysUserTab" class="table" data-click-to-select="true" data-mobile-responsive="true">
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/js/content.min.js?v=1.0.0"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="${pageContext.request.contextPath}/js/plugins/layer/layer.min.js"></script>
<%-- <script src="${pageContext.request.contextPath}/js/business/sysuser/sysuser_list.js"></script> --%>
<script src="${pageContext.request.contextPath}/js/public/ttbj-core.js"></script>
<script type="text/javascript">
var $table = $("#sysUserTab");
$(function () {
    initTable();
    //条件查询
    $("#queryUserBtn").click(function () {
        $table.bootstrapTable('refresh');
    });

    $("#addBut").click(function () {
        initUserEdit(null);
    });
    
    //获取公司名称
    var comnum = $("#salesmanComnum").val();
    //如果公司名称不为nul和不为undefined，则加载信息
    if(!comnum && typeof(comnum) != 'undefined'){
    	$.ajax({
            url: "../company/getAllCompanyNumAndName.htm",
            cache: false,
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.status == 0) {
                	var data = result.data;
                	addOptions(data, "#salesmanComnum");
                    return;
                }
            }
        });
    }

});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../user/getAllSalesmanUser.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "编号",
                field: "salesmanNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "真实姓名",
                field: "salesmanRealName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "用户名",
                field: "salesmanName",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "入职时间",
            	field: "salesmanJoinTime",
            	align: "center",
            	valign: "middle"
            },
            {
                title: "所属公司",
                field: "companyDto.comnumName",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"联系方式",
                field:"salesmanPhone",
                align: 'center',
                valign: 'middle'
            },
            {
            	title:"目标业绩(元)",
            	field:"salesmanGoal",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"奖金(元)",
            	field:"salesmanBonus",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"业务员类型",
            	field:"salesmanType",
            	align: 'center',
            	formatter: function(value, row, index){
            		var goal = "";
            		if(row.salesmanType == 1){
            			goal = "开单";
            		}else if(row.salesmanType == 2){
            			goal = "库管";
            		}
            		return goal;
            	}
            },
            
            {
            	title:"用户角色",
            	field:"roleDto.descr",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"地址",
            	field:"salesmanAddr",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"状态",
            	field:"salesmanIsDelete",
            	align: 'center',
            	formatter: function(value, row, index){
            		var state = "";
            		if(row.salesmanIsDelete == 0){
            			state = '<span style="color:#23c6c8;">启用</span>';
            		}else if(row.salesmanIsDelete == 1){
            			state = '<span style="color:#ed5565;">禁用</span>';
            		}
            		return state;
            	}
            },
            {
                title: "操作",
                field: "id,username",
                align: 'center',
                formatter: function (value, row, index) {
                	<shiro:hasPermission name="user:edit">
	                    return viewButInfo(row);
                    </shiro:hasPermission>
                    <shiro:lacksPermission name="user:edit">
	                    //companyDto.comnumName 得到的值已经修改，改为'-'
	                   	if(row.companyDto.comnumName == '-' || row.roleDto.roleName == 'admin'){
	   	                    return viewButInfo(row);
	                   	}
                   	</shiro:lacksPermission>   
                }
            }
        ]
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		salesmanName: $.trim($('#salesmanName').val()),
		companyName: $.trim($('#company').val()),
		salesmanIsDelete: $.trim($('#salesmanIsDelete').val()),
	};
	return temp;
}

function initUserEdit(id, salesmanComnum){
    var title;
    if(id){
        title = '编辑用户';
    }else{
        title = '新增用户';
    }
    layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['1000px' , '800px'],
        content: '../user/edit_sysuser.htm?id='+id,
        success: function(layero, index){
        }
    });
}

function deleteSysUser(userId, name,state) {
    layer.confirm('你确定要禁用或启用【' + name + '】用户状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../user/delSalesmanInfo.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'salesmanId': userId,'isDel':state},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../user/getAllSalesmanUser.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}

//显示的button按钮
function viewButInfo(row){
	var e = '<button class="btn btn-xs btn-info" type="button" onclick="initUserEdit(\'' + row.salesmanId + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
    var d = '';
    if (row.salesmanIsDelete == 0){
        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteSysUser(\'' + row.salesmanId + '\',\'' + row.salesmanName + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
    }else if(row.salesmanIsDelete == 1){
        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteSysUser(\'' + row.salesmanId + '\',\'' + row.salesmanName + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
    }
    return e + d;
}

//comnum的下拉列表  追加到option中
function addOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumNum + '">' + data[i].comnumName + '</option>');
	}
}
</script>
</body>
</html>
